import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login.vue'
import index from '@/views/index.vue'
import image from '@/views/bg/image.vue'
import user from '@/views/user/user.vue'
import role from '@/views/user/role.vue'
import basemenu from '@/views/device/basemenu.vue'
import base from '@/views/device/base.vue'
import list from '@/views/device/list.vue'
import property from '@/views/device/property.vue'
import add from '@/views/buycheck/addbuycheck.vue'
import checklist from '@/views/buycheck/buycheck.vue'
import buyback from '@/views/buycheck/buyback.vue'
import PatrolPoint from "@/views/patrol/PatrolPoint.vue"
import PatrolTask from "@/views/patrol/PatrolTask.vue"
import Patrolplan from "@/views/patrol/patrolplan.vue"
import patrolCheck from '@/views/patrol/patrolCheck.vue'

Vue.use(VueRouter)


const routes = [{
    path: "/",
    name: "/login",
    component: login
  },
  {
    path: "/login",
    name: "/login",
    component: login
  },
  {
    path: "/index",
    name: "index",
    redirect: "/image",
    component: index,
    children: [{
        path: "/image",
        name: "image",
        component: image
      },
      {
        path: "/user/manager",
        name: "user",
        component: user
      },
      {
        path: "/role/manager",
        name: "role",
        component: role
      }, {
        path: "/device/basemenu",
        name: "basemenu",
        component: basemenu,
        children: [{
          path: "/device/base",
          name: "base",
          component: base
        }]
      },
      {
        path: "/device/list",
        name: "list",
        component: list
      },
      {
        path: "/device/property",
        name: "property",
        component: property
      },
      {
        path: "/buy/add",
        name: "add",
        component: add
      },
      {
        path: "/buy/list",
        name: "list",
        component: checklist
      },
      {
        path: "/buy/buyback",
        name: "buyback",
        component: buyback
      },
      {
        path: "/patrol/item",
        name: "item",
        component: PatrolPoint
      },
      {
        path: "/polling/task",
        name: "task",
        component: PatrolTask
      },
      {
        path: "/patrol/plan",
        name: "plan",
        component: Patrolplan
      },
      {
        path: "/patrol/check",
        name: "patrolCheck",
        component: patrolCheck
      },
      {
        path: "/echarts",
        name: "echarts",
        component: () => import("@/views/echarts/echarts.vue")
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  let user = localStorage.getItem('user');
  let loginInfo = JSON.parse(user);
  if (loginInfo != null || to.path === "/login") {
    next();
  } else {
    next("/login");
  }
});

export default router